package com.lindroy.androidplayer.logic

import androidx.lifecycle.liveData
import com.lindroy.androidplayer.logic.model.ArticleResp
import com.lindroy.androidplayer.logic.network.HttpNetwork
import com.orhanobut.logger.Logger
import kotlinx.coroutines.Dispatchers
import kotlin.coroutines.CoroutineContext

/**
 * @author Lin
 * @date 2021/4/6
 * @function
 */
object Repository {

    //region 首页模块
    fun getArticlesList(page: Int) = fire(Dispatchers.IO) {
        val articlesAll = arrayListOf<ArticleResp>()
        if (page == 0) {
            val topArticles = HttpNetwork.getTopArticles()
            if (topArticles.success) {
                if (topArticles.data.isNotEmpty()) {
                    articlesAll.addAll(topArticles.data.map {
//                        it.isTop = true
                        it
                    })
                }
            }
        }
        val articleList = HttpNetwork.getHomeArticles(page)
        if (articleList.success) {
            if (articleList.data.datas.isNotEmpty()) {
                articlesAll.addAll(articleList.data.datas)
            }
            Result.success(articlesAll)
        } else {
            Result.failure(RuntimeException("response status is error"))
        }
    }

    /**
     * 获取轮播图
     */
    fun getBanners() = fire(Dispatchers.IO) {
        val bannersResult = HttpNetwork.getBanners()
        if (bannersResult.success) {
            val banners = bannersResult.data
            Result.success(banners)
        } else {
            Result.failure(RuntimeException("response status is error"))
        }
    }


    fun getTopArticles() = fire(Dispatchers.IO) {
        val topArticlesResult = HttpNetwork.getTopArticles()
        if (topArticlesResult.success) {
            val topArticles: List<ArticleResp> = topArticlesResult.data
            Result.success(topArticles)
        } else {
            Result.failure(RuntimeException("response status is ${topArticlesResult.errorCode}"))
        }
    }
    //endregion 首页模块


    //region 公众号模块
    //公众号目录
    fun getWXChapters() = fire(Dispatchers.IO) {
        val chaptersResult = HttpNetwork.getWXChapters()
        if (chaptersResult.success) {
            Result.success(chaptersResult.data)
        } else {
            Result.failure(RuntimeException(chaptersResult.errorMsg))
        }
    }

    //某一公众号下的文章
    fun getWXArticles(id: Int, page: Int) = fire(Dispatchers.IO) {
        val articlesResult = HttpNetwork.getWXArticles(id, page)
        if (articlesResult.success) {
            Result.success(articlesResult.data.datas)
        } else {
            Result.failure(RuntimeException(articlesResult.errorMsg))
        }
    }
    //endregion 公众号模块

    //region 项目模块
    fun getProjectTypes() = fire(Dispatchers.IO) {
        val projectResult = HttpNetwork.getProjectTypes()
        if (projectResult.success) {
            Result.success(projectResult.data)
        } else {
            Result.failure(RuntimeException(projectResult.errorMsg))
        }
    }

   /* fun getProjectList(id: Int, page: Int) = fire(Dispatchers.IO) {
        val result = HttpNetwork.getProjectList(id, page)
        if (result.success) {
            Result.success(result.data.datas)
        } else {
            Result.failure(RuntimeException(result.errorMsg))
        }
    }*/
    //endregion 项目模块

    //region 登录登出模块
    fun login(name: String, pwd: String) = fire {
        val result = HttpNetwork.login(name, pwd)
        if (result.success) {
            Result.success(result.data)
        }else{
            Result.failure(RuntimeException(result.errorMsg))
        }
    }
    //endregion 登录登出模块

    fun register(name:String,pwd:String,repwd:String) = fire{
        val result = HttpNetwork.register(name,pwd,repwd)
        if (result.success){
            Result.success(result.data)
        }else{
            Result.failure(RuntimeException(result.errorMsg))
        }
    }


    private fun <T> fire(
        context: CoroutineContext = Dispatchers.IO,
        block: suspend () -> Result<T>
    ) =
        liveData(context) {
            val result = try {
                block()
            } catch (e: Exception) {
                Logger.t("HttpRequest").e("请求出错：$e")
                Result.failure<T>(e)
            }
            //TODO(这一步不是很理解)
//            Logger.t("HttpRequest").e("请求结果：$result")
            emit(result)
        }

//    private fun  <T> failure() :Result{
//       return Result.failure<T>(RuntimeException("response status is error"))
//    }
}